package edu.njust.controller;

import edu.njust.Service.consumer.ConsumerService;
import edu.njust.controller.depcontroller.Code;
import edu.njust.controller.depcontroller.Result;
import edu.njust.domain.Consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ConsumerController")
public class ConsumerController {
    @Autowired
    private ConsumerService consumerService;

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable int id) {
        boolean flag = consumerService.deleteById(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    @PostMapping
    public Result insertAll(@RequestBody Consumer consumer) {
        boolean flag = consumerService.insertAll(consumer);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }

    @GetMapping("/{id}/{password}")
    public Result selectById(@PathVariable int id,@PathVariable String password) {
        Consumer consumer = consumerService.selectById(id,password);
        Integer code = consumer != null ? Code.GET_OK : Code.GET_ERR;
        String msg = consumer != null ? "" : "数据查询失败，请重试！";
        return new Result(code,consumer,msg);
    }

    @PutMapping
    public Result updateById(Consumer consumer) {
        boolean flag = consumerService.updateById(consumer);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    @GetMapping("/pageSelect/{name}/{start}/pageSize")
    public Result pageSelect(@PathVariable String name, @PathVariable int start,@PathVariable int pageSize) {
        List<Consumer> consumers=consumerService.pageSelect(name,start,pageSize);
        Integer code = consumers != null ? Code.GET_OK : Code.GET_ERR;
        String msg = consumers != null ? "" : "数据查询失败，请重试！";
        return new Result(code,consumers,msg);
    }

    @GetMapping("/countSelect/{name}")
    public Result countSelect(@PathVariable String name) {
        Integer num = consumerService.countSelect(name);
        Integer code = num != null ? Code.GET_OK : Code.GET_ERR;
        String msg = num != null ? "" : "数据查询失败，请重试！";
        return new Result(code,num,msg);
    }

    @GetMapping("/selectYZ/{papersNumber}")
    public Result selectYZ(@PathVariable String papersNumber) {
        boolean flag = consumerService.selectYZ(papersNumber);
        return new Result(Code.GET_OK,flag);
    }


}
